import sys

import pymysql
from PyQt5.QtWidgets import (QMainWindow, QDialog, QWidget, QAction, QHBoxLayout, QVBoxLayout, QGridLayout, QToolTip,
QPushButton, QMessageBox, QDesktopWidget, QApplication, QLabel, QTableWidget, QTableWidgetItem,
QFrame, QComboBox, QAbstractItemView, QHeaderView, QLineEdit)
from PyQt5.QtCore import (QCoreApplication, Qt, QRect, QSize)
from PyQt5.QtGui import (QIcon, QFont, QColor, QBrush, QTextCursor, QPixmap)
from PyQt5 import QtCore, QtGui, QtWidgets

class tableInfoUi(object):
    def setupUi(self, infoForm):
        infoForm.setObjectName("infoForm")
        infoForm.resize(1000, 400)
        infoForm.setWindowTitle("爬取公司详情")
        infoForm.setWindowIcon(QIcon('./images/spider.png'))
        infoForm.setStyleSheet("background-image:url(./images/blue4.png);")
        self.tableInfo = QtWidgets.QTableWidget(infoForm)
        self.tableInfo.setGeometry(QtCore.QRect(10, 20, 980, 380))
        self.tableInfo.setColumnCount(9)
        self.tableInfo.setHorizontalHeaderLabels(['公司名称', '爬取时间', '评分','人员规模','营业期限','注册地址','是否失信', '风险总数','爬取网站'])
        self.tableInfo.horizontalHeader().setStyleSheet(
            "QHeaderView::section{background-color:rgb(155, 194, 230);font:11pt '宋体';color: black;};")
        self.tableInfo.verticalHeader().setStyleSheet(
            "QHeaderView::section{background-color:rgb(155, 194, 230);font:11pt '宋体';color: black;};")
        self.tableInfo.setColumnWidth(0, 300)
        self.tableInfo.setColumnWidth(1, 250)
        self.tableInfo.setColumnWidth(2, 80)
        self.tableInfo.setColumnWidth(3, 150)
        self.tableInfo.setColumnWidth(4, 200)
        self.tableInfo.setColumnWidth(5, 100)
        self.tableInfo.setColumnWidth(6, 80)
        self.tableInfo.setColumnWidth(7, 80)
        self.tableInfo.setColumnWidth(8, 300)




def retranslateUi(self, infoForm):
    _translate = QtCore.QCoreApplication.translate
    infoForm.setWindowTitle(_translate("infoForm", "Form"))
    self.exitBtn.setText(_translate("infoForm", "退出"))



class tableInfoModel(QWidget):
    def __init__(self, id):
        super(tableInfoModel, self).__init__()
        self.tableId = id
        self.viewUi = tableInfoUi()
        self.main()

    def main(self):
        self.viewUi.setupUi(self)
        self.listData(self.tableId)
        #self.viewUi.exitBtn.clicked.connect(self.exit)
        self.show()
    def listData(self, id):
        id = str(id)
        conn = pymysql.connect(host="127.0.0.1", user="root", password="243420", database="enterprise",
                               charset="utf8")
        cursor = conn.cursor()
        sql ="select * from crawling_details where detail_id ="+id
        cursor.execute(sql)  # 执行sql语句
        results = cursor.fetchall()  # 获取查询的所有记录
        detail_id = []
        company_name = []
        crawling_time = []
        score = []
        personnel_size = []
        business_term = []
        register_address = []
        dishonor_flag = []
        total_risk = []
        crawling_url = []
        for row in results:
            detail_id.append(row[0])
            company_name.append(row[1])
            crawling_time.append(row[2])
            score.append(row[3])
            personnel_size.append(row[4])
            business_term.append(row[5])
            register_address.append(row[6])
            dishonor_flag.append(row[7])
            total_risk.append(row[8])
            crawling_url.append(row[9])

        self.viewUi.tableInfo.setRowCount(len(detail_id))
        # 循环输出数据库中的公司详情
        for i in range(0, len(detail_id)):
            newItem = QtWidgets.QTableWidgetItem(str(company_name[i]))
            newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 0, newItem)
            newItem1 = QtWidgets.QTableWidgetItem(str(crawling_time[i]))
            newItem1.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 1, newItem1)
            newItem2 = QtWidgets.QTableWidgetItem(str(score[i]))
            newItem2.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 2, newItem2)
            newItem3 = QtWidgets.QTableWidgetItem(str(personnel_size[i]))
            newItem3.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 3, newItem3)
            newItem4 = QtWidgets.QTableWidgetItem(str(business_term[i]))
            newItem4.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 4, newItem4)
            newItem5 = QtWidgets.QTableWidgetItem(str(register_address[i]))
            newItem5.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 5, newItem5)
            newItem6 = QtWidgets.QTableWidgetItem(str(dishonor_flag[i]))
            newItem6.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 6, newItem6)
            newItem7 = QtWidgets.QTableWidgetItem(str(total_risk[i]))
            newItem7.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 7, newItem7)
            newItem8 = QtWidgets.QTableWidgetItem(str(crawling_url[i]))
            newItem8.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
            self.viewUi.tableInfo.setItem(i, 8, newItem8)

        def exit(self):
            self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    tableInfoModel = tableInfoModel(1)
    sys.exit(app.exec_())
